package cn.myjerry.test.binaryTree;

public class BinaryTree {
	public static Node root;
	
	public void insert(int key) {
		Node newNode = new Node(key);
		if(root == null) {
			root = newNode;
		} else {
			insertNode(root, newNode);
		}
	}
	
	private static void insertNode(Node node, Node newNode) {
		if(node.getKey() > newNode.getKey()) {
			if(node.getLeft() == null) {
				node.setLeft(newNode);
			} else {
				insertNode(node.getLeft(), newNode);
			}
		} else {
			if(node.getRight() == null) {
				node.setRight(newNode);
			} else {
				insertNode(node.getRight(), newNode);
			}
		}
	}
	
	public String getRoot() {
		return root.toString();
	}
	
}

class Node {
	private int key;
	private Node left;
	private Node right;
	public Node() {
		super();
	}
	public Node(int key) {
		super();
		this.key = key;
	}
	public Node(int key, Node left, Node right) {
		super();
		this.key = key;
		this.left = left;
		this.right = right;
	}
	public int getKey() {
		return key;
	}
	public void setKey(int key) {
		this.key = key;
	}
	public Node getLeft() {
		return left;
	}
	public void setLeft(Node left) {
		this.left = left;
	}
	public Node getRight() {
		return right;
	}
	public void setRight(Node right) {
		this.right = right;
	}
}